﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System.Windows;
using ReflectionStudio.Core.FTPClient;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    using System.Data.OracleClient;
    using System.IO;
    public class JLTVKAV95Report : BaseReport
    {

        string sheetName = "";

        public bool IsOnAuditing = false;
        public bool IsSendForAuditing = false;
        Dictionary<string, int> qpplan = new Dictionary<string, int>();
        public bool IsApproved = false;
        public FTPClient ftpClient;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        INTEXDBC INTEXDBC;
        public JLTVKAV95Report()
            : base()
        {

        }
        public JLTVKAV95Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            UserID = (string)dicParamater["User"];
            Root = "C:\\IntexReport";
            INTEXDBC = new INTEXDBC(OracleDB, proxy);

        }
        public override void WriteExcelReport(string fileType)
        {
            try
            {
                Helper.GetDate(ref date, ref year, ref month, ref day);
                strExcelFile = @"JLT\JLTVKAV95.xls";
                Standard = "VKAV95";
                INTEXDBC.OracleDB = OracleDB;
                INTEXDBC.proxy = proxy;
                #region=================检查路径======================
                destfolder = SetFolder(Root, "订单跟踪");
                #endregion
                DataRow dwData = INTEXDB.SDDAT.NewRow();
                dwData["SDSA"] = "0013";
                dsSource.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));

                dsSource.Merge(INTEXDBC.QueryHRSTAMM(dwData, 3));

                #region==========准备程序===================
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strExcelFile);
                string ordernumer = date;
                exAgent.CheckWorkBook(ordernumer);
                #endregion
                ServerShareFile("", year, month, ordernumer, fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void SetTXTValue(string sgb, int x, int y)
        {
            int length = 0;
            string s1 = "";
            string s2 = "";
            try
            {
                s1 = sgb;
                exAgent.SetCellValue(x, y, s1);
                x++;
                return;
                length = sgb.Length;
                if (length <= 50)
                {
                    s1 = sgb;
                    exAgent.SetCellValue(x, y, s1);
                    x++;
                    return;
                }
                s1 = sgb.Substring(0, 50);
                exAgent.SetCellValue(x, y, s1);
                x++;
                s2 = sgb.Substring(50, length - 50);
                exAgent.SetCellValue(x, y, s2);
                x++;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void FillInExcelData(ArrayList columns, int xStart = 1, int yStart = 0)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = dsSource.Tables["ICC_VKAV95"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("VKPAUF", typeof(System.String));
                dtOutput.Columns.Add("PRODFACT", typeof(System.String));
                dtOutput.Columns.Add("VKPFART", typeof(System.String));
                dtOutput.Columns.Add("VKPFARBE", typeof(System.String));
                dtOutput.Columns.Add("STRUCTURE", typeof(System.String));
                dtOutput.Columns.Add("FWIDTH", typeof(System.String));
                dtOutput.Columns.Add("VKPMENGE1", typeof(System.String));
                dtOutput.Columns.Add("VKPMEEH", typeof(System.String));
                dtOutput.Columns.Add("VKPTOL", typeof(System.String));
                dtOutput.Columns.Add("VKPLIEFTERM", typeof(System.String));
                dtOutput.Columns.Add("FENTRYDAT", typeof(System.String));
                dtOutput.Columns.Add("FQTY1", typeof(System.String));
                dtOutput.Columns.Add("FQTY1OVER", typeof(System.String));
                dtOutput.Columns.Add("VKPOPENDEL", typeof(System.String));
                dtOutput.Columns.Add("FQTY2", typeof(System.String));
                dtOutput.Columns.Add("FQTY2REASON", typeof(System.String));
                dtOutput.Columns.Add("VKPFRDAT", typeof(System.String));
                dtOutput.Columns.Add("VKPFREIGE", typeof(System.String));
                dtOutput.Columns.Add("FSTOCK", typeof(System.String));
                dtOutput.Columns.Add("VKKSACHBE", typeof(System.String));
                dtOutput.Columns.Add("VKPVERS", typeof(System.String));
                dtOutput.Columns.Add("REMARK", typeof(System.String));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);        
                exAgent.DataTableToExcel(dtOutput, 3, 1);

                int count = dtOutput.Rows.Count;
                //exAgent.SetCellValue(1, 1, 1);
                //exAgent.BatchConvert2Number(2, 6, count + 1, 7);
                //exAgent.BatchConvert2Number(2, 9, count + 1, 9);
                //exAgent.SetCellValue(1, 1, "采购单号");

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void FillFooter(int x, string user)
        {
            exAgent.SetCellValue(x, 1, "营销人员:");
            exAgent.SetCellValue(x, 11, "工厂负责人:");
            exAgent.SetCellValue(x, 21, "制单:");
            exAgent.SetCellValue(x, 24, user);
            exAgent.SetCellValue(x, 31, "审核:");
        }

        private void ServerShareFile(string KRS, string year, string month, string ordernumer, string fileType)
        {
            try
            {
                string destfile, tempfile;
                ordernumer = "订单跟踪" + ordernumer;

                filename = destfolder + "\\" + ordernumer + ".xls"; ;
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                destfile = filename;
                tempfile = CreateFile(ordernumer);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);


                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private string CreateFile(string orderno)
        {
            string filename = "";
            try
            {
                string pwd = "";
                sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(orderno + ".xls", Standard, sheetName, pwd);
                filename = exAgent.FileName;
                //exAgent.SetCurrentSheet(2);
                //exAgent.RenameSheet("物测计划");
                exAgent.SetCurrentSheet(1);
                FillInExcelData(columns, 0, 0);
                exAgent.Save();
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }
    }
}
